using System.ComponentModel;
using System.Runtime.Serialization;

using SharpGL.Enumerations;

namespace SharpGL.OpenGLAttributes
{
    /// <summary>
	/// This class has all the settings you can edit for transformation matrix.
	/// </summary>
	[TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
    [DataContract(/*IsReference = true, */Name = "TransformAttributes", Namespace = "SharpGL.SceneGraph")]
    public class TransformAttributes : OpenGLAttributes
	{
        #region Private Consts

        private static readonly AttributeMask attributeMask = AttributeMask.Transform;

        #endregion Private Consts

        #region Private Fields

        [DataMember()]
        private bool? enableNormalize;
        [DataMember()]
        private MatrixMode? matrixMode;

        #endregion Private Fields

        #region Protected Methods

        protected override void OnDeserializing()
        {
            AttributeFlags = attributeMask;
        }

        #endregion Protected Methods

        #region Public Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="TransformAttributes"/> class.
        /// </summary>
        public TransformAttributes()
        {
            AttributeFlags = attributeMask;

            /*TODO
             Coefficients of the six clipping planes

            Enable bits for the user-definable clipping planes
            */
        }

        #endregion Public Constructors

        #region Public Methods

        /// <summary>
        /// Returns true if any attributes are set.
        /// </summary>
        /// <returns>
        /// True if any attributes are set
        /// </returns>
        public override bool AreAnyAttributesSet()
        {
            return
                enableNormalize.HasValue ||
                matrixMode.HasValue;
        }

        /// <summary>
        /// Reset all attributes to unset state.
        /// </summary>
        public override void ResetAllAttributes()
        {
            enableNormalize = null;
            matrixMode = null;
        }

        /// <summary>
        /// Sets the attributes.
        /// </summary>
        /// <param name="gl">The OpenGL instance.</param>
        public override void SetAttributes(OpenGL gl)
        {
            if (enableNormalize.HasValue) gl.EnableIf(OpenGL.GL_NORMALIZE, enableNormalize.Value);
            if (matrixMode.HasValue) gl.MatrixMode(matrixMode.Value);
        }

        #endregion Public Methods

        #region Public Properties

        /// <summary>
        /// Gets or sets the enable normalize.
        /// </summary>
        /// <value>
        /// The enable normalize.
        /// </value>
        [Description("EnableNormalize"), Category("Transform attributes")]
        public bool? EnableNormalize
		{
            get { return enableNormalize; }
            set
            {
                if (enableNormalize != value)
                {
                    enableNormalize = value;

                    NotifyPropertyChanged("EnableNormalize");
                }
            }
		}

        /// <summary>
        /// Gets or sets the matrix mode.
        /// </summary>
        /// <value>
        /// The matrix mode.
        /// </value>
        [Description("MatrixMode"), Category("Transform attributes")]
        public MatrixMode? MatrixMode
        {
            get { return matrixMode; }
            set
            {
                if (matrixMode != value)
                {
                    matrixMode = value;

                    NotifyPropertyChanged("MatrixMode");
                }
            }
        }

        #endregion Public Properties
    }
}